package com.puhui.lib.http.retrofit;

import android.content.Context;


import okhttp3.HttpUrl;
import okhttp3.Request;

/**
 * Copyright: 人人普惠
 * Created by TangJian on 2018/5/13.
 * Description:
 * Modified:
 */
public class ParamsContext {
    private IRequestParam iRequestParam;
    private Context context;
    private Request request;

    public ParamsContext(Request request, Context context) {
        this.context = context;
        this.request = request;
    }

    public Request getInRequest() {
        switch (request.method()) {
            case "GET":
                iRequestParam = new GetRequestParams();
                break;
            case "POST":
                iRequestParam = new PostRequestParams();
                break;
            default:
                iRequestParam = new PostRequestParams();
                break;
        }
        return iRequestParam.getRequest(request, context);
    }

    public interface IRequestParam {
        Request getRequest(Request request, Context context);
    }

    //get
    public class GetRequestParams implements IRequestParam {
        @Override
        public Request getRequest(Request request, Context context) {
            //添加公共参数
            HttpUrl modifiedUrl = request.url().newBuilder()
//                    .addQueryParameter("devId", BaseApplication.getInstance().getBaseParamInfo().getIMEI())
                    .addQueryParameter("devType", "2")  //1、ios，2、android
                    .addQueryParameter("pageSize", "10")
                    .build();

            return request.newBuilder().url(modifiedUrl).build();
        }
    }

    //post
    public class PostRequestParams implements IRequestParam {
        @Override
        public Request getRequest(Request request, Context context) {
//            if (request.body() instanceof FormBody) {
//                FormBody.Builder bodyBuilder = new FormBody.Builder();
//
//                FormBody formBody = (FormBody) request.body();
//
//                for (int i = 0; i < formBody.size(); i++) {
//                    bodyBuilder.addEncoded(formBody.encodedName(i), formBody.encodedValue(i));
//                }
//                formBody = bodyBuilder.addEncoded(CLIENT, AppTools.getClient(context))
//                        .addEncoded(VERSION, AppTools.getAppVersion(context))
//                        .addEncoded(TOKEN, AppTools.getToken(context))
//                        .build();
//                request = request.newBuilder().post(formBody).build();
//            }
            return request;
        }
    }
}
